package leetcode.array;

import sun.applet.Main;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created by xinxuchu on 2020/7/22.
 * 88. 合并两个有序数组
 */
public class MergeArray {

    static public void merge(int[] nums1, int m, int[] nums2, int n) {

        //现将数组合并，然后进行排序
        for (int i = 0; i < n; i++) {
            nums1[i + m] = nums2[i];
        }
        //排序开始,排序必须用双for循环
        for (int i = 0; i < nums1.length - 1; i++) {
            for (int j = i + 1; j < nums1.length; j++) {
                if (nums1[i] > nums1[j]) {
                    int tmp = nums1[i];
                    nums1[i] = nums1[j];
                    nums1[j] = tmp;
                }
            }
        }
    }

    static public void merge1(int[] nums1, int m, int[] nums2, int n) {

        //现将数组合并，然后进行排序
        for (int i = 0; i < n; i++) {
            nums1[i + m] = nums2[i];
        }

        Arrays.sort(nums1);
    }


    //
    public static void main(String[] args) {

        int[] num1 = new int[]{4, 5, 6, 0, 0, 0};
        int m = 3;
        int[] num2 = new int[]{1, 2, 3};
        int n = 3;
        merge(num1, m, num2, n);
//        merge1(num1,m,num2,n);

        System.out.println(Arrays.toString(num1));
    }
}
